<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a><cite>活动管理</cite></a>
    <a lay-href="lottery/index">抽奖活动</a>
    <a><cite>抽奖记录</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layadmin-search">
      <form class="layui-form" method="post" lay-filter="form_lottery_record_search" id="filterForm">
        <script type="text/html" template lay-done="layui.data.done(d.data)">
          <div class="layui-inline">
            <div class="layui-form-label">关键字搜索</div>
            <div class="layui-input-inline">
              <input type="text" name="keyword" value="{{decodeURIComponent(layui.router().search.keyword || '')}}"
                     class="layui-input"
                     placeholder="请输入手机号">
            </div>
          </div>
          <div class="layui-inline">
            <div class="layui-form-label">奖品</div>
            <div class="layui-input-inline">
              <select name="award_id">
                <option value="">全部</option>
              </select>
            </div>
          </div>
          <div class="layui-inline layui-btn-group">
            <button type="submit" lay-submit class="layui-btn" lay-filter="submit_lottery_record_search">
              <i class="layui-icon layui-icon-search"></i>查询
            </button>
            <button type="submit" lay-submit class="layui-btn layui-btn-warm"
                    lay-filter="reset_lottery_record_search">
              <i class="layui-icon layui-icon-close"></i>重置
            </button>
            <button type="submit" lay-submit class="layui-btn layui-btn-normal" lay-filter="export">
              <i class="layui-icon layui-icon-export"></i>导出
            </button>
          </div>
          <a class="layui-btn layui-table-link" id="btn_upload_delivery">
            <i class="layui-icon layui-icon-upload-circle"></i>批量发放
          </a>
        </script>
      </form>
    </div>
    <div class="layui-card-body">
      <div id="table_lottery_record"></div>
      <script type="text/html" id="tpl_user">
        {{d.user.mobile || ''}}
        {{# if (d.auth) { }}
        ({{d.auth.real_name}})
        {{# } else if (d.user.nickname) { }}
        ({{d.user.nickname || ''}})
        {{# } }}
      </script>
      <script type="text/html" id="tpl_award">
        {{# if (d.award) { }}
        {{d.award.name}}
        {{# } else  { }}
        <span class="layui-font-red">未中奖</span>
        {{# } }}
      </script>
      <script type="text/html" id="tpl_status">
        {{# if (d.award_id === 0) { }}
        <span class="layui-font-gray">-</span>
        {{# } else { }}
        {{# if (d.status.value === 'sent') { }}
        <span class="layui-font-green">已发放</span>
        {{# } }}
        {{# if (d.status.value === 'waiting') { }}
        <span class="layui-font-red">未发放</span>
        {{# } }}
        {{# } }}
      </script>
      <script type="text/html" id="tpl_tool">
        {{# if (d.award && d.award.type === 'entity') { }}
        <a class="layui-table-link" lay-event="deliver">
          {{# if (d.status.value === 'waiting') { }}
          <i class="layui-icon layui-icon-gift"></i>实物奖品发放
          {{# } }}
          {{# if (d.status.value === 'sent') { }}
          <i class="layui-icon layui-icon-edit"></i>修改物流信息
          {{# } }}
        </a>
        {{# } }}
      </script>
    </div>
  </div>
</div>

<script>
  layui.data.done = function () {
    layui.use(['fox_table'], function () {
      let $ = layui.$
        , fox_table = layui.fox_table
        , admin = layui.admin
        , form = layui.form
        , view = layui.view
        , upload = layui.upload
        , lottery_id = layui.router().search.id

      admin.get('agent-admin/lotteries/' + lottery_id + '/awards', function (res) {
        res.data.forEach((award) => {
          let opt = new Option(award.name, award.id)
          let selectedAwardId = layui.router().search.award_id
          if (award.id == selectedAwardId) {
            opt.selected = true
          }
          $('select[name="award_id"]').append(opt)
        })
        form.render();
      })

      let resourceUrl = 'agent-admin/lottery_records?lottery_id=' + lottery_id

      // 列表查询
      fox_table.render({
        id: 'table_lottery_record'
        , url: resourceUrl
        , cols: [[
          {title: '用户', templet: '#tpl_user', align: "left", minWidth: 180}
          , {title: '奖品', templet: '#tpl_award', width: 200, align: "left"}
          , {title: '状态', templet: '#tpl_status', width: 100, align: "center"}
          , {title: '参与时间', field: 'created_at', width: 180, align: "center"}
          , {title: '操作', width: 180, align: 'center', fixed: 'right', toolbar: '#tpl_tool'}
        ]]
      });

      fox_table.toolbar(function (obj) {
        let data = obj.data;
        if (obj.event === 'deliver') {
          showDeliverForm(data)
        }
      });

      //搜索
      form.on('submit(submit_lottery_record_search)', function (d) {
        let data = d.field
        data.id = lottery_id
        fox_table.reload(data);
        return false;
      });
      //重置
      form.on('submit(reset_lottery_record_search)', function (d) {
        $("input[name='keyword']").val('');
        $("select[name='award_id']").val('');
        form.render()
        fox_table.reload({id: lottery_id});
        return false;
      });
      //导出
      form.on('submit(export)', function (d) {
        let data = d.field
        if (!data.award_id) {
          layer.msg('只能按奖品进行导出，请先选择奖品')
          return false
        }
        data.lottery_id = lottery_id
        admin.post('agent-admin/lottery_records/export', data, function (res) {
          layer.confirm('数据下载中，请到下载中心查看下载任务.', function (index) {
            layer.close(index);
            location.hash = '/ops/download';
            return false;
          });
          layer.closeAll('loading');
        })
        return false;
      });

      //发货数据批量导入
      upload.render({
        elem: '#btn_upload_delivery'
        , url: 'agent-admin/lotteries/' + lottery_id + '/import_delivery' //上传接口
        , exts: 'xls|xlsx'
        , headers: {
          Authorization: admin.getAccessToken('Bearer')
        }
        , done: function (res) {
          if (res.code !== 0) {
            layer.msg(res.msg);
            return false;
          }
          layer.msg('物流信息更新成功');
          fox_table.reload()
          return false;
        }
      });

      function showDeliverForm(data) {
        data = data || {};
        let minWidth = '400px';
        let minHeight = '250px';
        admin.popup({
          title: '实物奖品发放'
          , area: [minWidth, minHeight]
          , id: 'popup_deliver'
          , success: function (layero, index) {
            view(this.id).render('lottery/deliver', data.remark || {}).done(function () {
              form.on('submit(submit_deliver)', function (d) {
                let field = d.field;
                admin.post('agent-admin/lottery_records/' + data.id + '/deliver', field, function (res) {
                  fox_table.reload();
                  layer.close(index)
                })
              })
            })
          }
        })
      }

    })
  }
</script>

